package com.cupk.controller;

import com.cupk.entity.Movie;
import com.cupk.entity.User;
import com.cupk.service.LoginService;
import com.cupk.service.MovieService;
import com.cupk.service.UserService;
import com.cupk.util.StringTools;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.HashMap;

/**
 * @author LY
 * @version 1.0
 */
@WebServlet("/Rating")
public class RatingServlet extends HttpServlet {
    MovieService movieService = new MovieService();
    UserService userService = new UserService();

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //打分
        String method = req.getParameter("method");
        String userIdStr = req.getParameter("userId");
        Integer userId = Integer.parseInt(userIdStr);
        String movieIdStr = req.getParameter("ID");
        Integer movieId = Integer.parseInt(movieIdStr);
        HttpSession session = req.getSession();

        User user = userService.findById(userId);
        if (method.equals("rating")) {   //打分
            String rateStr = req.getParameter("rate");
            String gradeStr = req.getParameter("grade");
            String numberStr = req.getParameter("number");

            Integer record = 0; //用户的历史评分
            Integer number = Integer.parseInt(numberStr);
            String records = user.getRecords();//从数据库中获取用户历史评分
            if (records != null) {    //用户历史评分不为空
                HashMap<Integer,Integer> newRecord = StringTools.func(records);
                if (newRecord.get(movieId) == null) {  //之前无记录
                    number += 1;
                } else {  //之前有记录
                    record = newRecord.get(movieId);
                    String delete = "<" + movieIdStr + ":" + record + ">";
                    records = records.replaceAll(delete, "");
                }
                records = records + "<" + movieIdStr + ":" + rateStr + ">";
            } else {//用户历史评分为空
                records = "<" + movieIdStr + ":" + rateStr + ">";
                number += 1;
            }
            userService.renewRecords(records, userId);
            Integer rate = Integer.parseInt(rateStr);
            Double grade = Double.parseDouble(gradeStr) - record + rate;
            movieService.updateGrade(movieId, grade, number);
        } else if (method.equals("collect")) { //收藏
            String collectible = user.getCollectible();
            if(collectible==null){  //收藏记录为空
                collectible = "<" + movieIdStr + ">";
            } else if(collectible.indexOf("<"+movieIdStr+">") == -1){   //未收藏过该电影
                collectible = collectible + "<" + movieIdStr + ">";
            }
            userService.addCollectible(userId,collectible);
        }
        Movie movie = movieService.findById(movieId);
        if(movie.getComment()!=null){   //之前有评论
            String[] comments = StringTools.func3(movie.getComment());
            req.setAttribute("comments", comments);
        }
        req.setAttribute("movie", movie);
        session.setAttribute("user", userService.findById(userId));
        req.getRequestDispatcher("result.jsp").forward(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("UTF-8");
        String movieIdstr = req.getParameter("movieId");
        Integer movieId = Integer.parseInt(movieIdstr);
        String viewpoint = req.getParameter("viewpoint");
        Movie movie = movieService.findById(movieId);
        if (movie.getComment() == null) {
            viewpoint = "<" + viewpoint + ">";
        } else {
            viewpoint = movie.getComment() + "<" + viewpoint + ">";
        }
        movieService.addComments(viewpoint, movieId);    //添加新评论

        movie = movieService.findById(movieId);
        String[] comments = StringTools.func3(movie.getComment());
        req.setAttribute("comments", comments);
        req.setAttribute("movie", movie);
        req.getRequestDispatcher("result.jsp").forward(req, resp);
    }
}
